package com.qiang.work.test03;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.xml.parsers.SAXParser;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Demo {
    public static void main(String[] args) throws DocumentException {

        //创建解析器
        SAXReader sr = new SAXReader();

        //加载(读取)文件
        Document read = sr.read(new File("day18\\student.xml"));

        //获取根标签
        Element rootElement = read.getRootElement();
        //获取指定子标签
        List<Element> Elements = rootElement.elements("student");

        //创建一个集合，用来封装学生对象
        ArrayList<Student> students = new ArrayList<>();
        for (Element element : Elements) {

            //获取id属性
            Attribute id = element.attribute("id");
            String value = id.getValue();

            Element name = element.element("name");
            String text = name.getText();
            Element sex = element.element("sex");
            String text1 = sex.getText();

            Student student = new Student(value, text, text1);

            students.add(student);
        }
        for (Student student : students) {
            System.out.println(student);
        }

    }
}
